﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using com.tiyma.components;
using com.tiyma.common;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Text;

namespace com.tiyma.web
{
    public partial class SEOChapter : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
          
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string t_key = txtChapterName.Text.Trim();

            t_key = DESEncryptBYTriple.Encrypt(t_key);

            ChapterContentManager t_chapter_mgr = new ChapterContentManager();

            try
            {
                string t_chapter_content = t_chapter_mgr.SEOChapter(t_key);

                t_chapter_content = t_chapter_content.Replace("#", "\"");
                t_chapter_content = t_chapter_content.Replace("@", "'");

                lblResult.Text = t_chapter_content;
            }
            catch (Exception ex)
            {
                LogHelper.Error("搜索小说章节内容失败" + ex.ToString());
            }

        }

        protected void btn_normal_search_Click(object sender, EventArgs e)
        {
            string t_key = txtChapterName.Text.Trim();

            t_key = DESEncryptBYTriple.Encrypt(t_key);

            ChapterContentManager t_chapter_mgr = new ChapterContentManager();

            try
            {
                string t_chapter_content = t_chapter_mgr.SEONChapter(t_key);

                t_chapter_content = t_chapter_content.Replace("#", "\"");
                t_chapter_content = t_chapter_content.Replace("@", "'");

                lblResult.Text = t_chapter_content;
            }
            catch (Exception ex)
            {
                LogHelper.Error("搜索小说章节内容失败" + ex.ToString());
            }
        }

        /// <summary>
        /// 测试的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_filter_word_Click(object sender, EventArgs e)
        {
            string t_content = "我是中国人baidu何必学外文百度，不懂abc百度，baidu革命接班人";
            //返回              我是中国人百度何必学外文baidu，不懂abcbaidu，百度革命接班人

            //构造数据库的配置的词
            DataSet t_ds = new DataSet();
            DataTable t_dtb = new DataTable();
            DataColumn t_col_yuanci = new DataColumn("yuanci", typeof(string));
            DataColumn t_col_tihuanci = new DataColumn("tihuanci", typeof(string));
            t_dtb.Columns.Add(t_col_yuanci);
            t_dtb.Columns.Add(t_col_tihuanci);

            DataRow t_row = t_dtb.NewRow();
            t_row["yuanci"] = "baidu";
            t_row["tihuanci"] = "百度";

            t_dtb.Rows.Add(t_row);

            t_ds.Tables.Add(t_dtb);

            string t_result = FilterContent(t_content, t_ds);


        }

        /// <summary>
        /// 过滤内容信息 【伪原创】
        /// </summary>
        /// <param name="i_content">文章内容</param>
        /// <param name="i_ds">过滤的关键词</param>
        /// <returns></returns>
        private string FilterContent(string i_content, DataSet i_ds)
        {
            if (i_ds == null || i_ds.Tables[0].Rows.Count == 0)
                return i_content;

            //原词
            string t_yuanci = string.Empty;
            //替换词
            string t_tihuanci = string.Empty;

            MatchCollection t_ma = null;

            List<ParseInfo> t_list = new List<ParseInfo>();

           Dictionary<int,int> t_dic=new Dictionary<int,int>();

            for (int i = 0; i < i_ds.Tables[0].Rows.Count; i++)
            {
                t_yuanci = i_ds.Tables[0].Rows[i]["yuanci"].ToString();
                t_tihuanci = i_ds.Tables[0].Rows[i]["tihuanci"].ToString();

                //把原词的位置记录下来
                t_ma = Regex.Matches(i_content, t_yuanci, RegexOptions.IgnoreCase | RegexOptions.Compiled);

                foreach (Match t_mc in t_ma)
                {
                    if (t_dic.ContainsKey(t_mc.Index))
                        continue;

                    t_dic.Add(t_mc.Index, t_mc.Index);

                    ParseInfo t_parse_info = new ParseInfo();

                    t_parse_info.WordIndex = t_mc.Index;
                    t_parse_info.OldWord = t_yuanci;
                    t_parse_info.NewWord = t_tihuanci;

                    t_list.Add(t_parse_info);
                }

                //把替换词的位置记录下来
                t_ma = Regex.Matches(i_content, t_tihuanci, RegexOptions.IgnoreCase | RegexOptions.Compiled);

                foreach (Match t_mc in t_ma)
                {
                    if (t_dic.ContainsKey(t_mc.Index))
                        continue;

                    t_dic.Add(t_mc.Index, t_mc.Index);

                    ParseInfo t_parse_info = new ParseInfo();

                    t_parse_info.WordIndex = t_mc.Index;
                    t_parse_info.OldWord = t_tihuanci;
                    t_parse_info.NewWord = t_yuanci;

                    t_list.Add(t_parse_info);
                }
            }

            if (t_list.Count == 0)
                return i_content;

            //按照需要被替换的词按照出现的顺序排序
            t_list.Sort(delegate(ParseInfo t_a, ParseInfo t_b)
            {
                if (t_a.WordIndex > t_b.WordIndex)
                    return 1;
                if (t_a.WordIndex == t_b.WordIndex)
                    return 0;

                return -1;
            });

            StringBuilder t_sb = new StringBuilder();

            //比如 content = 我是中国人baidu何必学外文百度，不懂abc百度，baidu革命接班人

            //先把【我是中国人】放到t_sb中
            t_sb.Append(i_content.Substring(0, t_list[0].WordIndex));

            //截取字符的开始位置
            int t_start_index = 0;

            //截取的字符串长度
            int t_length = 0;

            //把原词替换成替换词
            for (int index = 0; index < t_list.Count; index++)
            {
                //把【百度】放到t_sb中
                t_sb.Append(t_list[index].NewWord);

                t_start_index = t_list[index].WordIndex + t_list[index].OldWord.Length;

                if (index == t_list.Count - 1)
                {
                    if (t_start_index > i_content.Length)
                        break;

                    t_sb.Append(i_content.Substring(t_start_index));
                }
                else
                {
                    //【何必学外文】放到t_sb中
                    t_length = t_list[index + 1].WordIndex - t_list[index].WordIndex - t_list[index].OldWord.Length;

                    if (t_start_index + t_length >= i_content.Length)
                        break;

                    t_sb.Append(i_content.Substring(t_start_index, t_length));
                }
            }

            return t_sb.ToString();
        }
    }
}
